//
//  Model.swift
//  Space Experiment Module
//
//  Created by 曹亚索 on 2021/4/24.
//


import UIKit
import LeanCloud
var AQSwitch = false
var question : String!
var currentResult : String!
var resultArray : [String] = []
var testmode : Bool = false
let heart1 : UIImage! = #imageLiteral(resourceName: "heart1")
let heart2 : UIImage! = #imageLiteral(resourceName: "heart2")
let characterIdArray : [String] = ["tomato","chili","eggplant"]
var cct : Int! = 1
var currentCharacter : Int = 1
var currentScore : Int! = 0
var no1Score : Int! = 0
var no2Score : Int! = 0
var no3Score : Int! = 0
var no4Score : Int! = 0
var no5Score : Int! = 0
var assets : Int! = 0
var scoreArray : [Int] = []
let objectIdArray : [String] = ["60805f3e2a5bb23590bcf745","608060a9f5766f41e9c2a01d","608061482a5bb23590bcf810","6080614e2a5bb23590bcf812","608061522a5bb23590bcf813"]
let Tomato = Character()
let Chili = Character()
let Eggplant = Character()
var characterArray : [Character] = [Tomato,Chili,Eggplant]
var levelArray : [Any] = [Tomato.Level!,Chili.Level!,Eggplant.Level!]
var highestScore : Int! = 0
var healthViewArray : [UIImageView] = []

//MARK:角色属性
class Character{
    var Level : Int! = 1
    var image : UIImage!
    var insectImage : UIImage!
    var health : Int! = 1
}

class User{
    var userName : String!
    var assets : Int!
    var characterLevel : [Int]!
}

struct LevelUp {
    var level2Needed : Int = 300
    var level3Needed : Int = 800
}

struct QA {
    var question : String
    var answer : String
}

func setdata(){
    UserDefaults.standard.setValue(assets, forKey: "assets")
    UserDefaults.standard.setValue(Tomato.Level, forKey: "tomatolevel")
    UserDefaults.standard.setValue(Chili.Level, forKey: "chililevel")
    UserDefaults.standard.setValue(Eggplant.Level, forKey: "eggplantlevel")
}

func getdata(){
    if let data = UserDefaults.standard.string(forKey: "assets"){
        assets = Int(data)
    }
    if let data = UserDefaults.standard.string(forKey: "tomatolevel"){
        Tomato.Level = Int(data)
    }
    if let data = UserDefaults.standard.string(forKey: "chililevel"){
        Chili.Level = Int(data)
    }
    if let data = UserDefaults.standard.string(forKey: "eggplantlevel"){
        Eggplant.Level = Int(data)
    }
    if let data = UserDefaults.standard.string(forKey: "topscore"){
        highestScore = Int(data)
    }
}

func calcRank(num:Int){
    var push : Bool = false
    for score in scoreArray{
        //如成绩优于排行榜的成绩
        if num >= score{
            if push == false{
                push = true
                let ob = scoreArray.firstIndex(of: score)
                scoreArray.insert(num, at: ob!)
                pushArray(array: scoreArray)
            }
        }
    }
}

func pushArray(array:Array<Int>){
    do {
        let todo = LCObject(className: "Rank", objectId: "60a3b79efaa10e45649916fb")
        try todo.set("ScoreArray", value: array)
        todo.save { (result) in
            switch result {
            case .success:
                break
            case .failure(error: let error):
                print(error)
            }
        }
    } catch {
        print(error)
    }
}

func pushScore(num:Int){
    do {
        let todo = LCObject(className: "Rank", objectId: objectIdArray[num])
        try todo.set("Score", value: scoreArray[num])
            todo.save { (result) in
                switch result {
                case .success:
                    break
                case .failure(error: let error):
                    print(error)
                }
            }
    } catch {
        print(error)
    }
}
